package controll

import (
	"SalesMangent/function"
	"SalesMangent/models"

	"github.com/gin-gonic/gin"
)

func GetFactory(c *gin.Context) {
	db := function.GetDB()
	var factorydata []models.Factory
	db.Table("factory").Select("f_code, f_name").Order("f_code ASC").Find(&factorydata)
	c.JSON(200,factorydata)
}

func DelFactory(c *gin.Context) {
	db := function.GetDB()
	requestfactory := make(map[string]interface{})
	var factorydata models.Factory
	c.ShouldBind(&requestfactory)
	factorydata.Fcode = requestfactory["f_code"].(string)
	db.Table("factory").Where("f_code like ?", factorydata.Fcode).Delete(&factorydata)
	c.JSON(200, gin.H{
		"msg":"删除了下列数据",
		"f_code": factorydata.Fcode,
		"f_name": factorydata.Fname,
		"code": 200,
	})
}

func AddFactory(c *gin.Context) {
	db := function.GetDB()
	requestfactory := make(map[string]interface{})
	c.ShouldBind(&requestfactory)
	var factorydata models.Factory
	factorydata.Fcode = requestfactory["f_code"].(string)
	factorydata.Fname = requestfactory["f_name"].(string)
	db.Table("factory").Select("f_code", "f_name").Create(&factorydata)
	c.JSON(200, gin.H{
		"msg": "增加了下列信息",
		"f_code": factorydata.Fcode,
		"f_name": factorydata.Fname,
		"code": 200,
	})
}

func UpdateFactory(c *gin.Context) {
	db := function.GetDB()
	requestfactory := make(map[string]interface{})
	c.ShouldBind(&requestfactory)
	var factorydata models.Factory
	factorydata.Fcode = requestfactory["f_code"].(string)
	factorydata.Fname = requestfactory["f_name"].(string)
	db.Table("factory").Where("f_code = ?", factorydata.Fcode).Updates(&factorydata)
	c.JSON(200,gin.H{
		"msg": "更新了下列信息",
		"f_code": factorydata.Fcode,
		"f_name": factorydata.Fname,
		"code": 200,
	})
}